<?php

namespace addons\shopro\controller;


class Order extends Base
{

    protected $noNeedLogin = ['send_score','update_agent_level'];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $params = $this->request->get();

        $this->success('订单列表', \addons\shopro\model\Order::getList($params));
    }

    /**
     * 定时任务-每日发放积分
     * @return void
     */
    public function send_score()
    {
//        $a = $this->send_level_score(112,1000,153);
//        dump($a);exit;
        // 获取当前时间
        $currentHourMinute = date('H:i');
        //查询当前时间的订单
        $list = \addons\shopro\model\Order::field('id,user_id,send_score,send_score_day')
            ->where('status', 'not in',['-1','-2',0])
//            ->whereTime('paytime', 'today')
//            ->where('DATE_FORMAT(paytime, "%H:%i")', $currentHourMinute)
//            ->where('last_send_score_time', '<', strtotime(date('Y-m-d')))
            ->where('send_status',0)
            ->select();
        foreach ($list as $order) {
            $user = \addons\shopro\model\User::get($order->user_id);
            if(!$user){
                continue;
            }
            $send_score = $order->send_score;
            if($user->freezemoney < $order->send_score){
                $send_score = $user->freezemoney;
                $order->send_status = 1;
            }
           //释放积分
            if($send_score > 0){
                \addons\shopro\model\User::money($order->send_score, $user->id, 'sf',$order->id,'每天自动解冻积分');
                \addons\shopro\model\User::freezemoney(-$order->send_score, $user->id, 'sf',$order->id,'每天自动解冻积分');
            }
            $order->send_score_day = $order->send_score_day + 1;
            $order->last_send_score_time = time();
            $order->save();
            //发放直推、间推奖
            $this->send_zt_score($user,$order);
            //发放管理津贴
            $this->send_agent_score($user->parent_user_id, $order->send_score, $order->id,$user->id);
            //发放级差奖
//            $this->send_level_score($user->parent_user_id,$order->send_score,$order->id);
        }
        echo '执行成功';
    }

    /**
     * 发放级差奖
     */
    public function send_level_score($parent_user_id,$score,$order_id,$send_rate = 0)
    {
        $user = \addons\shopro\model\User::get($parent_user_id);
        if($user && $user->agent_level > 0){
            if($user->agent_level == 1){
                $send_rate =  0.004 - $send_rate;
            }elseif ($user->agent_level == 2){
                $send_rate = 0.008 - $send_rate;
            }elseif ($user->agent_level == 3){
                $send_rate = 0.012 - $send_rate;
            }elseif ($user->agent_level == 4){
                $send_rate = 0.016 - $send_rate;
            }elseif ($user->agent_level == 5){
                $send_rate = 0.02 - $send_rate;
            }
            $res_score = $score * $send_rate;
            if ($user->freezemoney < $res_score) {
                $res_score = $user->freezemoney;
            }
            if ($res_score > 0) {
//                \addons\shopro\model\User::money($res_score, $user->id, 'sf',$order_id,'获得级差奖励');
//                \addons\shopro\model\User::freezemoney(-$res_score, $user->id, 'sf',$order_id,'级差奖解冻积分');
            }

        }
        if ($user && $user->parent_user_id) {
            $this->send_level_score($user->parent_user_id, $score, $order_id,$send_rate);
        }


    }


    /**
     * 发放直推、间推奖
     */
    public function send_zt_score($user,$order)
    {
        //发放直推奖
        if($user->parent_user_id){
            $zt_score = $order->send_score*0.2;
            $p_info = \addons\shopro\model\User::get($user->parent_user_id);
            if($p_info->freezemoney < $zt_score){
                $zt_score = $p_info->freezemoney;
            }
            if($zt_score > 0){
                \addons\shopro\model\User::money($zt_score, $p_info->id, 'sf',$order->id,'直推解冻积分');
                \addons\shopro\model\User::freezemoney(-$zt_score, $p_info->id, 'sf',$order->id,'直推解冻积分');
            }
            //发放间推奖
            if($p_info->parent_user_id) {
                $jt_score = $order->send_score * 0.15;
                $pp_info = \addons\shopro\model\User::get($p_info->parent_user_id);
                if ($pp_info->freezemoney < $jt_score) {
                    $jt_score = $pp_info->freezemoney;
                }
                if ($jt_score > 0) {
                    \addons\shopro\model\User::money($jt_score, $pp_info->id, 'sf',$order->id,'间推解冻积分');
                    \addons\shopro\model\User::freezemoney(-$jt_score, $pp_info->id, 'sf',$order->id,'间推解冻积分');
                }
            }

        }
    }

    /**
     * 递归发放管理津贴
     */
    public function send_agent_score($parent_user_id, $score, $order_id,$last_id,$send_rate = 0)
    {
        $user = \addons\shopro\model\User::get($parent_user_id);
        if($user) {
            if($user->agent_level > 0){
                //查询当前会员是大团队还是小团队,如果是小团队则发管理奖
                $max_user_id = \addons\shopro\model\User::where('parent_user_id', $user->id)->order('total_yeji','desc')->value('id') ?? 0;
                if($max_user_id != $last_id && $max_user_id > 0){
//                    if($user->agent_level == 1){
//                        $res_score = $score * 0.05;
//                    }elseif ($user->agent_level == 2){
//                        $res_score = $score * 0.1;
//                    }elseif ($user->agent_level == 3){
//                        $res_score = $score * 0.15;
//                    }elseif ($user->agent_level == 4){
//                        $res_score = $score * 0.2;
//                    }elseif ($user->agent_level == 5){
//                        $res_score = $score * 0.25;
//                    }elseif ($user->agent_level == 6){
//                        $res_score = $score * 0.3;
//                    }elseif ($user->agent_level == 7){
//                        $res_score = $score * 0.4;
//                    }

                    if($user->agent_level == 1){
//                        if($send_rate != 0){
//                            $res_score = 0;
//                        }else{
//                            $res_score = $score *  0.05;
//                            $send_rate = $send_rate + 0.05;
//                        }
                        $res_score = $score * (0.05 - $send_rate);
                        if($res_score > 0 ){
                            $send_rate = $send_rate + 0.05;
                        }
                    }elseif ($user->agent_level == 2){
                        $res_score = $score * (0.1 - $send_rate);
                        if($res_score > 0 ){
                            $send_rate = $send_rate + 0.1;
                        }
                    }elseif ($user->agent_level == 3){
                        $res_score = $score * (0.15 - $send_rate);
                        if($res_score > 0 ){
                            $send_rate = $send_rate + 0.15;
                        }
                    }elseif ($user->agent_level == 4){
                        $res_score = $score * (0.2 - $send_rate);
                        if($res_score > 0 ){
                            $send_rate = $send_rate + 0.2;
                        }
                    }elseif ($user->agent_level == 5){
                        $res_score = $score * (0.25 - $send_rate);
                        if($res_score > 0 ){
                            $send_rate = $send_rate + 0.25;
                        }
                    }elseif ($user->agent_level == 6){
                        $res_score = $score * (0.3 - $send_rate);
                        if($res_score > 0 ){
                            $send_rate = $send_rate + 0.3;
                        }
                    }elseif ($user->agent_level == 7){
                        $res_score = $score * (0.4 - $send_rate);
                        if($res_score > 0 ){
                            $send_rate = $send_rate + 0.4;
                        }
                    }
                    if ($user->freezemoney < $res_score) {
                        $res_score = $user->freezemoney;
                    }
                    if($res_score > 0){
                        \addons\shopro\model\User::money($res_score, $user->id, 'sf',$order_id,'团队购买获得管理津贴解冻积分');
                        \addons\shopro\model\User::freezemoney(-$res_score, $user->id, 'sf',$order_id,'团队购买获得管理津贴解冻积分');
                    }
                }
            }

            if ($user && $user->parent_user_id) {
                $this->send_agent_score($user->parent_user_id, $score, $order_id,$user->id,$send_rate);
            }
        }
    }

    /**
     * 定时任务-更新用户等级
     * @return void
     */
    public function update_agent_level()
    {
        $user_list = \addons\shopro\model\User::field('id,agent_level')->where('agent_level', '<', 8)->select();
        foreach ($user_list as $user) {
            //查询此用户当前等级
            //查询下级最高的一个业绩
            $max_yeji = \addons\shopro\model\User::where('parent_user_id',$user->id)->max('total_yeji');
            //查询团队总业绩
            $total_yeji = \addons\shopro\model\User::where('parent_user_id',$user->id)->sum('total_yeji') - $max_yeji;
            $agent_level = 0;
            //区域代理
            if($max_yeji >= 100000 && $total_yeji >= 50000){
                $agent_level = 1;
            }
            //县级代理
            if($max_yeji >= 300000 && $total_yeji >= 100000){
                $agent_level = 2;
            }
            //市级代理
            if($max_yeji >= 2000000 && $total_yeji >= 500000){
                $agent_level = 3;
            }
            //省级代理
            if($max_yeji >= 6000000 && $total_yeji >= 1500000){
                $agent_level = 4;
            }
            //总代
            if($max_yeji >= 15000000 && $total_yeji >= 5000000){
                $agent_level = 5;
            }
            //合伙人
            if($max_yeji >= 16000000 && $total_yeji >= 6000000){
                $agent_level = 6;
            }
            //初级合伙人
            if($max_yeji >= 18000000 && $total_yeji >= 7000000){
                $agent_level = 7;
            }
            if($user->agent_level < $agent_level){
                $user->agent_level = $agent_level;
                $user->save();
            }
        }
    }



    public function detail()
    {
        $params = $this->request->get();
        $this->success('订单详情', \addons\shopro\model\Order::detail($params));
    }


    public function itemDetail()
    {
        $params = $this->request->get();
        $this->success('订单商品', \addons\shopro\model\Order::itemDetail($params));
    }


    // 即将废弃
    public function statusNum()
    {
        $this->success('订单数量', \addons\shopro\model\Order::statusNum());
    }


    // 取消订单
    public function cancel()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'cancel');

        $this->success('取消成功', \addons\shopro\model\Order::operCancel($params));
    }

    // 删除订单
    public function delete()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'delete');

        $this->success('删除成功', \addons\shopro\model\Order::operDelete($params));
    }

    // 确认收货
    public function confirm()
    {
        repeat_filter();        // 防抖
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'confirm');

        $this->success('收货成功', \addons\shopro\model\Order::operConfirm($params));
    }


    public function comment()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'comment');

        $this->success('评价成功', \addons\shopro\model\Order::operComment($params));
    }


    public function pre()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'pre');

        $result = \addons\shopro\model\Order::pre($params);

        if (isset($result['msg']) && $result['msg']) {
            $this->error($result['msg'], $result);
        } else {
            $this->success('计算成功', $result);
        }
    }


    public function createOrder()
    {
        repeat_filter();        // 防抖
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'createOrder');

        $order = \addons\shopro\model\Order::createOrder($params);

        $this->success('订单添加成功', $order);
    }


    // 获取可用优惠券列表
    public function coupons()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'coupons');

        $coupons = \addons\shopro\model\Order::coupons($params);

        $this->success('获取成功', $coupons);
    }
}
